package com.tarena.demo.luban.all.main.controller;


import com.tarena.demo.luban.all.main.service.OrderService;
import com.tarena.demo.luban.commons.restful.JsonResult;
import com.tarena.demo.luban.protocol.order.param.OrderAddParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/base/order")
@Api(tags = "订单")
public class OrderController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
   /* @Autowired
    private OrderService orderService;*/
    @PostMapping("/add")
    @ApiOperation("新增订单的功能")
    public JsonResult addOrder(OrderAddParam orderAddParam){
        //1. 组织创建message
        Message<OrderAddParam> message = MessageBuilder.withPayload(orderAddParam).build();
        //2. 使用异步调用发送消息 order-test-topic
        rocketMQTemplate.asyncSend("order-test-topic", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功");
            }
            @Override
            public void onException(Throwable e) {
                //TODO 发送如果失败的，需要处理么？
                System.out.println("发送失败");
            }
        });
        /*Long start=new Date().getTime();
        orderService.addOrder(orderAddParam);
        Long end=new Date().getTime();
        Long gap=start-end;*/
        //TODO 返回新增订单完成 实际完成了么？和前端交互逻辑 有变化
        return JsonResult.ok("新增订单完成!");
    }
}
